export class GuidData {
    public readonly key?: string;
    public readonly tips?: string;
    // public readonly step: number = 0;
    // 当某一步,需要隐藏引导对话时,暂停执行下一步.
    public readonly guidHide?: boolean = false;
    public readonly tryBtnText?: string;
}

export class GuidHelper {
    public static get(): GuidHelper {
        return this._instance;
    }

    private static _instance: GuidHelper = new GuidHelper();

    private guids: { [key: string]: GuidData } = {};

    private _isGuiding: boolean = false;
    private currentStep: number = 0;
    private _guidChangeListener: Function = null;
    private stepKeys: number[] = [];

    public startGuid(guids: {}) {
        this._isGuiding = true;
        this.currentStep = 0;
        this.guids = guids;

        this.stepKeys = Object.keys(guids).map(v => {
            return parseInt(v);
        }).sort((a, b) => {
            return a - b;
        });
        this.onStepChange();
    }

    public isGuiding(): boolean {
        return this._isGuiding;
    }

    public setGuidListener(callback: (step: number, d: GuidData) => void) {
        this._guidChangeListener = callback;
    }

    public nextGuid() {
        if (!this._isGuiding) {
            return;
        }
        this.currentStep++;
        if (this.currentStep >= this.stepKeys.length) {
            this.endGuid();
        } else {
            this.onStepChange();
        }
    }

    public checkGuid(step: number) {
        if (this.stepKeys[this.currentStep] == step) {
            this.nextGuid();
        }
    }

    public endGuid() {
        this._isGuiding = false;
        this.onStepChange();
        this._guidChangeListener = null;
    }

    private onStepChange() {
        if (this._guidChangeListener) {
            const step = this.stepKeys[this.currentStep];
            this._guidChangeListener(step || 0, this.guids[step]);
        }
    }
}
